package com.xyy.smsmsm;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import cn.smssdk.EventHandler;
import cn.smssdk.SMSSDK;

public class RegisterActivity extends AppCompatActivity implements View.OnClickListener {

    String APPKEY = "1b1f2e138bf3c";

    String APPSECRETE = "07aaf34c3465755d86fbff43b05593af";
    //电话号码输入框

    private EditText mPhoneEdt;

//验证码输入框

    private EditText mCodeEdt;

//发送验证码按钮

    private Button mCodeRequestBtn;

//注册按钮

    private Button mRegisterBtn;

    private int i = 30;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_register);
        init();

    }

    /**
     * 初始化控件
     */

    private void init() {

        mPhoneEdt = (EditText) findViewById(R.id.input_phone_et);

        mCodeEdt = (EditText) findViewById(R.id.input_code_et);

        mCodeRequestBtn = (Button) findViewById(R.id.request_code_btn);

        mRegisterBtn = (Button) findViewById(R.id.register_btn);

        mCodeRequestBtn.setOnClickListener(this);

        mRegisterBtn.setOnClickListener(this);

        SMSSDK.initSDK(this, APPKEY, APPSECRETE);

        EventHandler eventHandler = new EventHandler() {

            @Override

            public void afterEvent(int event, int result, Object data) {

                Message message = new Message();

                message.arg1 = event;

                message.arg2 = result;

                message.obj = data;

                handler.sendMessage(message);

            }

        };
        //注册回调监听接口

        SMSSDK.registerEventHandler(eventHandler);
    }

    @Override
    public void onClick(View view) {

        String mIphone = mPhoneEdt.getText().toString();
        switch (view.getId()) {
            case R.id.request_code_btn:
                if (judgePhoneNums(mIphone)) {
                    SMSSDK.getVerificationCode("86", mIphone);

                    mCodeRequestBtn.setClickable(false);

                    mCodeRequestBtn.setText("重新发送(" + i + ")");
                    new Thread(new Runnable() {
                        @Override
                        public void run() {
                            for (; i > 0; i--) {
                                handler.sendEmptyMessage(1);
                                if (i <= 0) {

                                    break;

                                }
                                try {
                                    Thread.sleep(1000);
                                } catch (InterruptedException e) {
                                    e.printStackTrace();
                                }
                            }
                            handler.sendEmptyMessage(2);


                        }
                    }).start();
                    break;
                } else {

                }
                break;
            case R.id.register_btn:
                SMSSDK.submitVerificationCode("86", mIphone, mCodeEdt.getText().toString());

                break;
        }


    }

    /**
     * 判断手机号码是否合理
     *
     * @paramphoneNums
     */
    private boolean judgePhoneNums(String phoneNums) {
        if (isMatchLength(phoneNums, 11)

                && isMobileNO(phoneNums)) {

            return true;

        }

        Toast.makeText(this, "手机号码输入有误！", Toast.LENGTH_SHORT).show();

        return false;
    }

    /**
     * 判断一个字符串的位数
     *
     * @return
     * @paramstr
     * @paramlength
     */
    public static boolean isMatchLength(String str, int length) {

        if (str.isEmpty()) {

            return false;

        } else {

            return str.length() == length ? true : false;

        }

    }

    /**
     * 验证手机格式
     */

    public static boolean isMobileNO(String mobileNums) {
        String telRegex = "[1][358]\\d{9}";// "[1]"代表第1位为数字1，"[358]"代表第二位可以为3、5、8中的一个，"\\d{9}"代表后面是可以是0～9的数字，有9位。

        if (TextUtils.isEmpty(mobileNums))

            return false;
        else
            return mobileNums.matches(telRegex);

    }

    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            if (msg.what == 1) {

                mCodeRequestBtn.setText("重新发送" + i + "s");

            } else if (msg.what == 2) {

                mCodeRequestBtn.setText("获取验证码");

                mCodeRequestBtn.setClickable(true);

            } else {
                int event = msg.arg1;

                int result = msg.arg2;

                Object data = msg.obj;

                Log.e("event", "event=" + event);
                if (result == SMSSDK.RESULT_COMPLETE) {





                    //回调完成
                    if (event == SMSSDK.EVENT_SUBMIT_VERIFICATION_CODE) {
                        //提交验证码成功

                        Toast.makeText(getApplicationContext(), "提交验证码成功",

                                Toast.LENGTH_SHORT).show();

                        Intent intent = new Intent(RegisterActivity.this,

                                MainActivity.class);

                        startActivity(intent);
                    } else if (event == SMSSDK.EVENT_GET_VERIFICATION_CODE) {
                        //获取验证码成功

                        Toast.makeText(getApplicationContext(), "正在获取验证码",

                                Toast.LENGTH_SHORT).show();
                    } else if (event == SMSSDK.EVENT_GET_SUPPORTED_COUNTRIES) {
                        //返回支持发送验证码的国家列表
                    }
                }

            }
        }
    };


    @Override
    protected void onDestroy() {
        SMSSDK.unregisterAllEventHandler();
        super.onDestroy();
    }
}
